package com.wz.device.controller;


import com.wz.device.entity.BasAreaSpace;
import com.wz.device.entity.BasCargoArea;
import com.wz.device.entity.BasCargoSpace;
import com.wz.device.service.BasCargoAreaService;
import com.wz.device.service.BasCargoSpaceService;
import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.project.system.role.service.IRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qiqi
 * @since 2021-10-22
 */
@Controller
@RequestMapping("/device/cargoArea")
public class BasCargoAreaController extends BaseController {

    private String prefix = "device/basCargoArea";

    @Autowired
    private BasCargoAreaService basCargoAreaService;
    @Autowired
    private IRoleService roleService;

//    @Autowired
//    private IPostSevice postService;

    @Autowired
    private BasCargoSpaceService basCargoSpaceService;


//    @RequiresPermissions("device:cargoarea:view")
    @GetMapping()
    public String basCargoArea() {
        return prefix + "/basCargoArea";
    }

    /**
     * 设备库房区域信息列表
     * @param basCargoArea
     * @return
     */
//    @RequiresPermissions("device:cargoarea:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasCargoArea basCargoArea){
        startPage();
        List<BasCargoArea> list = basCargoAreaService.selectBasCargoAreaList(basCargoArea);
        return getDataTable(list);
    }


    /**
     * 新增设备库区管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 设备库房区域增加
     * @param basCargoArea
     * @return
     */
    @RequiresPermissions("device:cargoarea:add")
    @Log(title = "设备库房区域管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated BasCargoArea basCargoArea)
    {
        return new AjaxResult(AjaxResult.Type.SUCCESS, basCargoAreaService.insertBasCargoArea(basCargoArea));
    }

    /**
     * 修改设备库房信息
     */
    @RequiresPermissions("device:cargoarea:edit")
    @GetMapping("/edit/{cargo_area_id}")
    public String edit(@PathVariable("cargo_area_id") String cargo_area_id, ModelMap mmap)
    {
        mmap.put("cargo_area", basCargoAreaService.selectBasCargoAreaById(cargo_area_id));
        return prefix + "/edit";
    }

    /**
     * 修改保存设备库区
     */
    @RequiresPermissions("device:cargoarea:edit")
    @Log(title = "设备库区管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated BasCargoArea basCargoArea)
    {
        return toAjax(basCargoAreaService.updateBasCargoArea(basCargoArea));

    }

    /**
     * 修改设备库房信息
     */
    @RequiresPermissions("device:cargoarea:modify")
    @GetMapping("/modify/{cargo_area_id}")
    public String modify(@PathVariable("cargo_area_id") String cargo_area_id, ModelMap mmap)
    {
        BasCargoSpace basCargoSpace = new BasCargoSpace();
        startPage();
        List<BasCargoSpace> list = basCargoSpaceService.selectBasCargoSpaceList(basCargoSpace);
        mmap.put("cargo_area", list);
        mmap.put("cargo_area_id",cargo_area_id);
        return prefix + "/modify";
    }


    /**
     * 修改货位库区
     */
    @Log(title = "修改货位库区", businessType = BusinessType.UPDATE)
    @RequiresPermissions("device:cargoarea:modify")
    @PostMapping("/modify")
    @ResponseBody
    public AjaxResult modifySave(@RequestBody BasAreaSpace basAreaSpace)
    {
        if (basAreaSpace.getCargoAreaId().isEmpty() || basAreaSpace.getIds().size()==0){
            return toAjax(false);
        }
        return toAjax(basCargoSpaceService.updataCargoAreaId(basAreaSpace.getCargoAreaId(),basAreaSpace.getIds()));
    }



    @RequiresPermissions("device:cargoarea:remove")
    @Log(title = "设备库区管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basCargoAreaService.deleteCargoAareByIds(ids));
    }

    /**
     * 库区是否锁定修改
     */
    @Log(title = "设备库区管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("device:cargoarea:edit")
    @PostMapping("/changeLocked")
    @ResponseBody
    public AjaxResult changeLocked(BasCargoArea basCargoArea)
    {
        return toAjax(basCargoAreaService.changeLocked(basCargoArea));
    }
}
